home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / 3d game controls / source / aevt.c next >
Encoding:
Text File  |  1996-06-29  |  5.3 KB  |  224 lines

  1. //--------------------------------------------------------------------------------------------
  2. //  Event Handling Code
  3. //
  4. //      by Philip McBride
  5. //        code taken freely from Apple DTS sample code
  6. //
  7. //--------------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <AppleEvents.h>
  11. #include <Drag.h>
  12. #include <GestaltEqu.h>
  13. #include <Memory.h>
  14. #include <SegLoad.h>
  15. #include <StandardFile.h>
  16.  
  17. #include "GameControls.h"
  18. #include "AEVT.h"
  19. #include "extern.h"
  20. #include "file.h"
  21. #include "document.h"
  22. #include "event.h"
  23.  
  24.  
  25. //--------------------------------------------------------------------------------------------
  26. //  Handle High Level Event
  27. //
  28. void DoHighLevelEvent(EventRecord *ev)
  29. {
  30.     OSErr err;
  31.     
  32.     err = AEProcessAppleEvent(ev);    
  33. }
  34.  
  35.  
  36. //--------------------------------------------------------------------------------------------
  37. //  Handle Open Application
  38. //
  39. pascal OSErr MyAEHandleOAPP(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  40. {
  41.     OSErr err = noErr ;
  42.     SFTypeList            theTypeList = {'3DMF'};
  43.     StandardFileReply    theReply;
  44.             
  45.     *theAppleEvent = *theAppleEvent;
  46.      *reply = *reply;
  47.      refCon = refCon;
  48.     
  49.     StandardGetFile(0L, 1, theTypeList, &theReply);
  50.     
  51.     // if we had a good file reply, then handle the file opening.
  52.     if (theReply.sfGood)
  53.         HandleOpenDoc(&theReply.sfFile);
  54.  
  55.     return err;
  56. }
  57.  
  58.  
  59. //-----------------------------------------------------------------------
  60. // Handle Open Document
  61. //
  62. pascal OSErr MyAEHandleODOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  63. {
  64.     FSSpec         myFSS;
  65.     AEDescList    docList;
  66.     OSErr        err,
  67.                 ignoreErr;
  68.     long        index,
  69.                 itemsInList;
  70.     Size         actualSize;
  71.     AEKeyword    keywd;
  72.     DescType    returnedType;
  73.  
  74.     *theAppleEvent = *theAppleEvent;
  75.      *reply = *reply;
  76.      refCon = refCon;
  77.      
  78.     err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList);
  79.     if (err == noErr) {
  80.     
  81.         // see how many descriptor items are in the list
  82.         // this is the number of documents we want to open
  83.         err = AECountItems(&docList,&itemsInList);
  84.  
  85.         // now get each descriptor record from the list
  86.         // coerce the returned data to an FSSpec record, and
  87.         // open the asoociated file
  88.         
  89.         for (index=1; index <= itemsInList && err == noErr; index++) {
  90.         
  91.             err = AEGetNthPtr(    &docList, 
  92.                                 index,
  93.                                 typeFSS,
  94.                                 &keywd,
  95.                                 &returnedType,
  96.                                 (Ptr)&myFSS,
  97.                                 sizeof(myFSS),
  98.                                 &actualSize);
  99.     
  100.             if (err == noErr) {
  101.             
  102.                 FInfo        fndrInfo ;
  103.                 
  104.                 // we now have a valid FSSpec to reference the file, we need to know 
  105.                 // what type the file is to determine which file open function to call
  106.                 // we can determine this from the finder info for the file
  107.                 
  108.                 err = FSpGetFInfo( &myFSS, &fndrInfo );    
  109.                 
  110.                 // if we got that ok, then we switch on the file  
  111.                 // type (we don't care about the creator type)    
  112.                         
  113.                 if (err == noErr)    {
  114.                 
  115.                     switch( fndrInfo.fdType ) {
  116.                         case 'TEXT':
  117.                         case '3DMF':
  118.                             HandleOpenDoc(&myFSS);
  119.                             break ;
  120.                         default:
  121.                             MySendQuitApp();
  122.                             break ;
  123.                     }
  124.                 }
  125.             }
  126.         }
  127.         ignoreErr = AEDisposeDesc(&docList);
  128.     }
  129.     return err ;
  130. }
  131.  
  132. //-----------------------------------------------------------------------
  133. // Handle Print Document
  134. //
  135. pascal OSErr MyAEHandlePDOC(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  136. {
  137.     // not using these variables
  138.     theAppleEvent = theAppleEvent;
  139.     reply = reply;
  140.     refCon = refCon;
  141.     
  142.     return noErr;
  143. }
  144.  
  145. //--------------------------------------------------------------------------------------------
  146. //  Handle Quit
  147. //
  148. pascal OSErr MyAEHandleQUIT(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  149. {
  150.     OSErr             err = noErr ;        // used as return value
  151.     WindowPtr        theWindow;
  152.     
  153.     // not using these variables
  154.     theAppleEvent = theAppleEvent;
  155.     reply = reply;
  156.     refCon = refCon;
  157.  
  158.     gQuitting = true;
  159.         
  160.     // attempt to close the document if window left open
  161.     theWindow = (WindowPtr)FrontWindow();
  162.     if (theWindow)
  163.         MyCloseDocument((DocumentPtr ) (((WindowPeek) theWindow)->refCon));        
  164.  
  165.     // if we closed everything up successfully, we can return noErr, otherwise
  166.     // indicate to sender of the 'quit' aevt that we canceled
  167.     
  168.     if (gQuitting) {
  169.         gQuit = true;                        // user didn't cancel
  170.         err = AEDisposeDesc(&pSelfAddress);    // Dispose of my self-addressed descriptor.
  171.     }
  172.     else {
  173.         err = userCanceledErr ;
  174.     }
  175.             
  176.     return err ;
  177. }
  178.  
  179. //--------------------------------------------------------------------------------------------
  180. //  Send a Quit Message
  181. //
  182. void MySendQuitApp( void )
  183. {
  184.     AppleEvent    myAppleEvent, reply;
  185.     OSErr    err;
  186.     
  187.     //    Create the Apple Event.
  188.     err = AECreateAppleEvent( kCoreEventClass, 
  189.                                   kAEQuitApplication, 
  190.                                   &pSelfAddress,
  191.                                   kAutoGenerateReturnID, 
  192.                                   kAnyTransactionID, 
  193.                                   &myAppleEvent);
  194.                                   
  195.     //    Send the Apple Event.
  196.       err = AESend( &myAppleEvent, 
  197.                         &reply, 
  198.                         kAENoReply+kAENeverInteract, 
  199.                         kAENormalPriority,
  200.                         kAEDefaultTimeout, 
  201.                         nil, 
  202.                         nil);
  203.                         
  204.       AEDisposeDesc(&myAppleEvent);                // Dispose of the Apple Event.
  205. }
  206.  
  207. //--------------------------------------------------------------------------------------------
  208. //  Send an Open Document Message
  209. //
  210. void MySendOpenDoc(FSSpec *myFSSpec)
  211. {
  212.     myFSSpec = myFSSpec;
  213. }
  214.  
  215. //--------------------------------------------------------------------------------------------
  216. //  Send a Print Message
  217. //
  218. void MySendPrintDoc(FSSpec *myFSSpec)
  219. {
  220.     myFSSpec = myFSSpec;
  221.  
  222.  
  223.